//
//  CHPopverController.m
//  仿IPad的pop功能
//
//  Created by lili on 16/8/24.
//  Copyright © 2016年 陈浩. All rights reserved.
//

#import "CHPopverController.h"
#define UIScreenW [UIScreen mainScreen].bounds.size.width
@interface CHPopverController()

@property(nonatomic,strong)UIImageView *popverView;
// 内容控制器
@property(nonatomic,strong)UIViewController *contentVc;

@end

@implementation CHPopverController


-(instancetype)initWithContentViewController:(UIViewController *)viewController{
    if (self = [super init]) {
        
        self.contentVc = viewController;
        
        
        
        UIImageView *popverView = [[UIImageView alloc] init];
        //        popverView.backgroundColor = [UIColor redColor];
        // 设置图片的交互
        popverView.userInteractionEnabled = YES;
        
        //图片要拉伸
        popverView.image = [self resizeImgWithName:@"popover_background"];
        
        // PopView添加内容
        [popverView addSubview:viewController.view];
        
        self.popverView = popverView;
        
        
        //        // 设置尺寸
        //        popverView.bounds = CGRectMake(0, 0, 100, 100);
        
        
    }
    
    return self;
}

-(void)setPopoverContentSize:(CGSize)popoverContentSize{
    _popoverContentSize = popoverContentSize;
    
    // 设置popverView的尺寸
    
    // 顶部边距
    CGFloat topMargin = 15;
    //其它边距 左/底/右边
    CGFloat otherMargin = 10;
    CGFloat pvWidth = _popoverContentSize.width + 2 * otherMargin;
    CGFloat pvHeight = _popoverContentSize.height + topMargin + otherMargin;
    self.popverView.bounds = CGRectMake(0, 0, pvWidth, pvHeight);
    
    // 设置内容frm
    self.contentVc.view.frame = CGRectMake(otherMargin, topMargin, _popoverContentSize.width, _popoverContentSize.height);
    
}

-(void)presentPopoverInView:(UIView *)inView{
    //把视图添加到窗口上
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    
    // 显示之前，添加一个按钮
    UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    coverBtn.backgroundColor = [UIColor yellowColor];
    //    coverBtn.alpha = 0.5;
    coverBtn.frame = window.bounds;
    [coverBtn addTarget:self action:@selector(coverBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [window addSubview:coverBtn];
    
    // 中心点是固定死
    CGFloat centerX = UIScreenW * 0.5;
    CGFloat centerY = 64 +  self.popverView.frame.size.height * 0.5 - 10;
    self.popverView.center = CGPointMake(centerX, centerY);
    
    // 1.先获取inView 中心点
    CGPoint inViewCenter = inView.center;
    //    inViewCenter.y += 20;
    
    // 坐标系转换
    // 把从导航栏获取的中心点 转化成 在"窗口" 上中心点
    CGPoint popverCenter = [inView.superview convertPoint:inViewCenter toView:window];
    popverCenter.y += self.popverView.frame.size.height * 0.5;
    popverCenter.y += inView.frame.size.height * 0.5;
    NSLog(@"inViewCenter: %@",NSStringFromCGPoint(inViewCenter));
    NSLog(@": %@",NSStringFromCGPoint(popverCenter));
    
    self.popverView.center = popverCenter;
    
    [window addSubview:self.popverView];
    
}


-(void)presentPopoverInView:(UIView *)inView arrowDirection:(CHPopverArrowDirection)arrow{
    // 2.设置popverView的center
    [self presentPopoverInView:inView];
    
    CGPoint center = self.popverView.center;
    
    // 计算偏移值
    CGFloat delta = (self.popverView.frame.size.width - inView.frame.size.width) * 0.5;
    // 1.更改背景图片
    switch (arrow) {
        case CZPopverArrowDirectionCenter:
            self.popverView.image = [self resizeImgWithName:@"popover_background"];
            break;
        case CZPopverArrowDirectionLeft:
            self.popverView.image = [self resizeImgWithName:@"popover_background_left"];
            center.x += delta;
            self.popverView.center = center;
            break;
        case CZPopverArrowDirectionRight:
            self.popverView.image = [self resizeImgWithName:@"popover_background_right"];
            center.x -= delta;
            self.popverView.center = center;
            break;
            
    } 
}

-(void)coverBtnClick:(UIButton *)coverBtn{
    NSLog(@"%s",__func__);
    // 移除coverBtn
    [coverBtn removeFromSuperview];
    // 移除popver
    [self.popverView removeFromSuperview];
}


-(UIImage *)resizeImgWithName:(NSString *)imgName{
    UIImage *img = [UIImage imageNamed:imgName];
    return [img stretchableImageWithLeftCapWidth:img.size.width * 0.5 topCapHeight:img.size.height * 0.5];
}

@end
